package net.rain.common;

import org.jetbrains.annotations.NotNull;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static net.rain.utils.RainConstants.LOGIN_USER_KEY;

/**
 * @author Rainy-Heights
 * @version 1.0
 * @Date 2023/7/15 21:47
 */
public class LoginInterceptor implements HandlerInterceptor {

    private StringRedisTemplate stringRedisTemplate;

    public LoginInterceptor(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate=stringRedisTemplate;
    }

    @Override
    public boolean preHandle(@NotNull HttpServletRequest request, @NotNull HttpServletResponse response, Object handler) {
        //判断是否需要拦截

        if (stringRedisTemplate.opsForHash().get(LOGIN_USER_KEY+"c8204b3f2ef248de8657936f3c003af2","id")==null){
            response.setStatus(401);//未授权代码
            return false;
        }
        return true;
    }
}
